/*     */ package rtp;
/*     */ 
/*     */ import java.awt.Dimension;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import javax.media.Codec;
/*     */ import javax.media.Control;
/*     */ import javax.media.ControllerClosedEvent;
/*     */ import javax.media.ControllerEvent;
/*     */ import javax.media.ControllerListener;
/*     */ import javax.media.Format;
/*     */ import javax.media.Manager;
/*     */ import javax.media.MediaLocator;
/*     */ import javax.media.NoProcessorException;
/*     */ import javax.media.Owned;
/*     */ import javax.media.Player;
/*     */ import javax.media.Processor;
/*     */ import javax.media.control.QualityControl;
/*     */ import javax.media.control.TrackControl;
/*     */ import javax.media.format.VideoFormat;
/*     */ import javax.media.protocol.ContentDescriptor;
/*     */ import javax.media.protocol.DataSource;
/*     */ import javax.media.protocol.PushBufferDataSource;
/*     */ import javax.media.protocol.PushBufferStream;
/*     */ import javax.media.rtp.RTPManager;
/*     */ import javax.media.rtp.SendStream;
/*     */ import net.jxta.protocol.PipeAdvertisement;
/*     */ 
/*     */ public class RTPJxtaMulticastTransmit
/*     */ {
/*     */   private MediaLocator locator;
/*  40 */   private Processor processor = null;
/*     */   private RTPManager[] rtpMgrs;
/*  44 */   private DataSource dataOutput = null;
/*     */   private PipeAdvertisement dataPipeAdv;
/*     */   private PipeAdvertisement ctrlPipeAdv;
/* 338 */   private Integer stateLock = new Integer(0);
/* 339 */   private boolean failed = false;
/*     */ 
/*     */   public RTPJxtaMulticastTransmit(MediaLocator locator, PipeAdvertisement dataPipeAdv, PipeAdvertisement ctrlPipeAdv)
/*     */   {
/*  50 */     this.locator = locator;
/*  51 */     this.dataPipeAdv = dataPipeAdv;
/*  52 */     this.ctrlPipeAdv = ctrlPipeAdv;
/*     */   }
/*     */ 
/*     */   public synchronized String start()
/*     */   {
/*  66 */     String result = createProcessor();
/*  67 */     if (result != null) {
/*  68 */       return result;
/*     */     }
/*     */ 
/*  74 */     result = createTransmitter();
/*  75 */     if (result != null) {
/*  76 */       this.processor.close();
/*  77 */       this.processor = null;
/*  78 */       return result;
/*     */     }
/*     */ 
/*  83 */     this.processor.start();
/*     */ 
/*  85 */     return null;
/*     */   }
/*     */ 
/*     */   private String createProcessor()
/*     */   {
/*  92 */     if (this.locator == null) {
/*  93 */       return "Locator is null";
/*     */     }
/*     */ 		DataSource ds;
/*     */     try
/*     */     {
				
/*  99 */       ds = Manager.createDataSource(this.locator);
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/*     */       
/* 101 */       return "Couldn't create DataSource";
/*     */     }
/*     */     try
/*     */     {
/*     */       
/* 107 */       this.processor = Manager.createProcessor(ds);
/*     */     } catch (IOException ex) {
/* 109 */       return "IOException creating processor";
/*     */     } catch (NoProcessorException ex) {
/* 111 */       return "Couldn't create processor";
/*     */     }
/*     */ 
/* 116 */     boolean result = waitForState(this.processor, 180);
/* 117 */     if (!result) {
/* 118 */       return "Couldn't configure processor";
/*     */     }
/*     */ 
/* 123 */     TrackControl[] tracks = this.processor.getTrackControls();
/*     */ 
/* 127 */     if ((tracks == null) || (tracks.length < 1)) {
/* 128 */       return "Couldn't find tracks in processor";
/*     */     }
/*     */ 
/* 136 */     ContentDescriptor cd = new ContentDescriptor("raw.rtp");
/* 137 */     this.processor.setContentDescriptor(cd);
/*     */ 
/* 140 */     Format chosen = null;
/* 141 */     boolean atLeastOneTrack = false;
/*     */ 
/* 144 */     for (int i = 0; i < tracks.length; i++) {
/* 145 */       Format format = tracks[i].getFormat();
/* 146 */       if (tracks[i].isEnabled()) {
/* 147 */         Format[] supported = tracks[i].getSupportedFormats();
/*     */ 
/* 152 */         if (supported.length > 0) {
/* 153 */           if ((supported[0] instanceof VideoFormat))
/*     */           {
/* 156 */             chosen = checkForVideoSizes(tracks[i].getFormat(), supported[0]);
/*     */           }
/* 158 */           else chosen = supported[0];
/*     */ 
/* 160 */           tracks[i].setFormat(chosen);
/* 161 */           System.err.println("Track " + i + " is set to transmit as:");
/* 162 */           System.err.println("  " + chosen);
/* 163 */           atLeastOneTrack = true;
/*     */         } else {
/* 165 */           tracks[i].setEnabled(false);
/*     */         }
/*     */       } else {
/* 168 */         tracks[i].setEnabled(false);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 173 */     if (!atLeastOneTrack) {
/* 174 */       return "Couldn't set any of the tracks to a valid RTP format";
/*     */     }
/*     */ 
/* 181 */     result = waitForState(this.processor, 300);
/* 182 */     if (!result) {
/* 183 */       return "Couldn't realize processor";
/*     */     }
/*     */ 
/* 187 */     setJPEGQuality(this.processor, 0.1F);
/*     */ 
/* 191 */     this.dataOutput = this.processor.getDataOutput();
/*     */ 
/* 193 */     return null;
/*     */   }
/*     */ 
/*     */   private String createTransmitter()
/*     */   {
/* 205 */     PushBufferDataSource pbds = (PushBufferDataSource)this.dataOutput;
/* 206 */     PushBufferStream[] pbss = pbds.getStreams();
/*     */ 
/* 209 */     this.rtpMgrs = new RTPManager[pbss.length];
/* 210 */     for (int i = 0; i < pbss.length; i++) {
/*     */       try {
/* 212 */         this.rtpMgrs[i] = RTPManager.newInstance();
/* 213 */         this.rtpMgrs[i].initialize(new RTPJxtaMulticastSocketAdapter(this.dataPipeAdv, this.ctrlPipeAdv));
/*     */ 
/* 215 */         SendStream sendStream = this.rtpMgrs[i].createSendStream(this.dataOutput, i);
/* 216 */         sendStream.start();
/*     */       } catch (Exception e) {
/* 218 */         return e.getMessage();
/*     */       }
/*     */     }
/*     */ 
/* 222 */     return null;
/*     */   }
/*     */ 
/*     */   Format checkForVideoSizes(Format original, Format supported)
/*     */   {
/* 236 */     Dimension size = ((VideoFormat)original).getSize();
/* 237 */     Format jpegFmt = new Format("jpeg/rtp");
/* 238 */     Format h263Fmt = new Format("h263/rtp");
/*     */     int height;
              int width;
/* 240 */     if (supported.matches(jpegFmt))
/*     */     {
/* 243 */         width = size.width % 8 == 0 ? size.width : 
/* 244 */         size.width / 8 * 8;
/*     */ 
/* 246 */       height = size.height % 8 == 0 ? size.height : 
/* 247 */         size.height / 8 * 8;
/*     */     }
/*     */     else
/*     */     {
/*     */       
/* 248 */       if (supported.matches(h263Fmt))
/*     */       {
/*     */         
/* 250 */         if (size.width < 128) {
/* 251 */           width = 128;
/* 252 */           height = 96;
/*     */         }
/*     */         else
/*     */         {
/*     */           
/* 253 */           if (size.width < 176) {
/* 254 */             width = 176;
/* 255 */             height = 144;
/*     */           } else {
/* 257 */             width = 352;
/* 258 */             height = 288;
/*     */           }
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 264 */         return supported;
/*     */       }
/*     */     }
/*     */     
/*     */     
/* 268 */     return new VideoFormat(null, 
/* 269 */       new Dimension(width, height), 
/* 270 */       -1, 
/* 271 */       null, 
/* 272 */       -1.0F).intersects(supported);
/*     */   }
/*     */ 
/*     */   void setJPEGQuality(Player p, float val)
/*     */   {
/* 281 */     Control[] cs = p.getControls();
/* 282 */     QualityControl qc = null;
/* 283 */     VideoFormat jpegFmt = new VideoFormat("jpeg");
/*     */ 
/* 287 */     for (int i = 0; i < cs.length; i++)
/*     */     {
/* 289 */       if ((!(cs[i] instanceof QualityControl)) || 
/* 290 */         (!(cs[i] instanceof Owned))) continue;
/* 291 */       Object owner = ((Owned)cs[i]).getOwner();
/*     */ 
/* 295 */       if ((owner instanceof Codec)) {
/* 296 */         Format[] fmts = ((Codec)owner).getSupportedOutputFormats(null);
/* 297 */         for (int j = 0; j < fmts.length; j++) {
/* 298 */           if (fmts[j].matches(jpegFmt)) {
/* 299 */             qc = (QualityControl)cs[i];
/* 300 */             qc.setQuality(val);
/* 301 */             System.err.println("- Setting quality to " + 
/* 302 */               val + " on " + qc);
/* 303 */             break;
/*     */           }
/*     */         }
/*     */       }
/* 307 */       if (qc != null)
/*     */         break;
/*     */     }
/*     */   }
/*     */ 
/*     */   public void stop()
/*     */   {
/* 319 */     synchronized (this) {
/* 320 */       if (this.processor != null)
/*     */       {
/* 322 */         this.processor.stop();
/* 323 */         this.processor.close();
/* 324 */         this.processor = null;
/*     */ 
/* 327 */         for (int i = 0; i < this.rtpMgrs.length; i++) {
/* 328 */           this.rtpMgrs[i].removeTargets("Session ended.");
/* 329 */           this.rtpMgrs[i].dispose();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   Integer getStateLock()
/*     */   {
/* 342 */     return this.stateLock;
/*     */   }
/*     */ 
/*     */   void setFailed()
/*     */   {
/* 347 */     this.failed = true;
/*     */   }
/*     */ 
/*     */   private synchronized boolean waitForState(Processor p, int state)
/*     */   {
/* 353 */     p.addControllerListener(new StateListener());
/* 354 */     this.failed = false;
/*     */ 
/* 358 */     if (state == 180)
/* 359 */       p.configure();
/* 360 */     else if (state == 300) {
/* 361 */       p.realize();
/*     */     }
/*     */ 
/* 368 */     while ((p.getState() < state) && (!this.failed)) {
/* 369 */       synchronized (getStateLock()) {
/*     */         try {
/* 371 */           getStateLock().wait();
/*     */         } catch (InterruptedException ie) {
/* 373 */           return false;
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 379 */     return !this.failed;
/*     */   }
/*     */ 
/*     */   class StateListener
/*     */     implements ControllerListener
/*     */   {
/*     */     StateListener()
/*     */     {
/*     */     }
/*     */ 
/*     */     public void controllerUpdate(ControllerEvent ce)
/*     */     {
/* 396 */       if ((ce instanceof ControllerClosedEvent)) {
/* 397 */         RTPJxtaMulticastTransmit.this.setFailed();
/*     */       }
/*     */ 
/* 402 */       if ((ce instanceof ControllerEvent))
/* 403 */         synchronized (RTPJxtaMulticastTransmit.this.getStateLock())
/*     */         {
/* 405 */           RTPJxtaMulticastTransmit.this.getStateLock().notifyAll();
/*     */         }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\cpeer\ourvod_cpeer.jar
 * Qualified Name:     ourvod.rtp.RTPJxtaMulticastTransmit
 * JD-Core Version:    0.6.0
 */